<?php

class Controller_ClassNews extends Controller_Website {

    public function action_list() {
        $where = array();
        $where['status'] = Arr::get($_GET, 'status');
        $where['is_commend'] = Arr::get($_GET, 'is_commend');
        $where['cid'] = Arr::get($_GET, 'cid');
        $where['ORDER'] = 'id DESC';
        $where = array_filter($where, 'strlen');
        
        $title = Arr::get($_GET, 'title');
        if (!empty($title)) {
            $where['title'] = array('like'=>"%$title%");
        }
        
        $m_classNews = Model::factory('classNews');
        $total = $m_classNews->count($where);
        $pager = new Pager($total, 10);
        $list = $m_classNews->select($pager->offset, $pager->size, $where)->as_array();
        
        $m_category = Model::factory('classCategory');
        $articleCategory = $m_category->getAll();
        $this->content = View::factory('classNews/list');
        $this->content->list = $list;
        $this->content->pager = $pager;
        $this->content->articleCategory = $articleCategory;

    }

    public function action_add() {
        
        if (!empty($_POST)) {
            
            $data = $this->_get_data($_POST);
            $data['teacher_pic'] = $data['pic'][1];
            $data['pic'] = $data['pic'][0];
            $data['add_time'] = time();
            $m_classNews = Model::factory('classNews');
            $ret = $m_classNews->insert($data);
            if ($ret !== false) {
                $this->redirect('classNews/list');
            }
        }
        $m_category = Model::factory('classCategory');
        $m_video = Model::factory('classVideo');
        $video = $m_video->getAll()->as_array();
        $cat_list = $m_category->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $this->content = View::factory('classNews/add');
        $this->content->video = $video;
        $this->content->info = null;
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
    }

    public function action_edit() {
        $id = Arr::get($_GET, 'id');
        $m_classNews = Model::factory('classNews');
        $info = $m_classNews->getRowById($id);
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $ret = $m_classNews->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('classNews/list');
            }
        }
        
        $m_category = Model::factory('classCategory');
        $cat_list = $m_category->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        
        $this->content = View::factory('classNews/edit');
        $this->content->info = $info;
        
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
    }

    public function action_del() {
        $id = $_GET['id'];
        $m_classNews = Model::factory('classNews');
        $ret = $m_classNews->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    public function action_close() {
        $id = $_GET['id'];
        $data = array(
            'status' => '0',
        );
        $m_classNews = Model::factory('classNews');
        $ret = $m_classNews->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open() {
        $id = $_GET['id'];
        $data = array(
            'status' => '1',
        );
        $m_classNews = Model::factory('classNews');
        $ret = $m_classNews->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_close_commend() {
        $id = $_GET['id'];
        $data = array(
            'is_commend' => '0',
        );
        $m_classNews = Model::factory('classNews');
        $ret = $m_classNews->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open_commend() {
        $id = $_GET['id'];
        $data = array(
            'is_commend' => '1',
        );
        $m_classNews = Model::factory('classNews');
        $ret = $m_classNews->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    protected function _get_data($post) {
        $data = array_intersect_key($post, array_flip(array('cid','title','desc','pic','video_url','video_price','teacher','teacher_desc','class_desc','author')));
        
        $data['edit_time'] = strtotime('now');
        return $data;
    }
    
}
